//
//CSNateOAuthResult.m
//  CairnStory
//  www.cairnstory.com
//
//  Created by saturna on 11. 3. 22..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CSNateOAuthResult.h"
#import "NSString+MD5.h"



// oauth parameter key
NSString* const kNateKeyOAuthConsumerKey = @"oauth_consumer_key";
NSString* const kNateKeyOAuthConsumerSecret = @"oauth_consumer_secret";

NSString* const kNateKeyOAuthVersion = @"oauth_version";
NSString* const kNateKeyOAuthCallback = @"oauth_callback";
NSString* const kNateKeyOAuthSignatureMethod = @"oauth_signature_method";

NSString* const kNateKeyOAuthToken = @"oauth_token";
NSString* const kNateKeyOAuthTokenSecret = @"oauth_token_secret";
NSString* const kNateKeyOAuthCallbackConfirmed = @"oauth_callback_confirmed";
NSString* const kNateKeyOAuthVerifier = @"oauth_verifier";

NSString* const kNateKeyOAuthNonce = @"oauth_nonce";
NSString* const kNateKeyOAuthTimestamp = @"oauth_timestamp";
NSString* const kNateKeyOAuthSignature = @"oauth_signature";


#define kProfileKeyNateOn @"NateOnProfile"
#define kProfileKeyCLog @"CLogProfile"



@implementation CSNateOAuthResult

//@synthesize isAuthorized = m_isAuthorized;

- (void)dealloc
{
	[m_oauthParams release];
	[m_profile release];

	[super dealloc];
}


- (id)initWithDictionary:(NSDictionary*)params
{
	self = [super init];
	if (self == nil)
	{
		return self;
	}

	m_oauthParams = [params retain];
	m_profile = [[NSMutableDictionary alloc] init];

	NSMutableDictionary* nateOnProfile = [[NSMutableDictionary alloc] init];
	[m_profile setObject:nateOnProfile forKey:kProfileKeyNateOn];
	[nateOnProfile release];

	NSMutableDictionary* cLogProfile = [[NSMutableDictionary alloc] init];
	[m_profile setObject:cLogProfile forKey:kProfileKeyCLog];
	[cLogProfile release];

	return self;
}

- (NSString*)oauthParamForKey:(NSString*)paramKey
{
	return [m_oauthParams objectForKey:paramKey];
}

- (BOOL)isAuthorized
{
	return m_oauthParams != nil && [m_oauthParams count] > 0;
}

- (NSString*)description
{
	return [NSString stringWithFormat:@"<%@> {\n"
			"	OAuthParams : %@\n"
			"	Profile : %@\n"
			"}", [self class], m_oauthParams, m_profile];
}
/*
<CSNateOAuthResult> {
	OAuthParams : {
		CK = df4b2cdcb813acbfb3a328359272fe38;
		NK = 90b9fe53db4a4fb5fb83a6dfb30690a1;
		SSO = NC;
		"___oauth_token" = 2cb74eb50114333c249f0ddb0d102071;
		"oauth_callback" = "http://www.cairnstory.com/nate/callback";
		"oauth_callback_confirmed" = true;
		"oauth_consumer_key" = 889311ad190a8111aa545314834a531c04d89712f;
		"oauth_consumer_secret" = 4db2772f6ee27eac3d9f2b2713d08d0c;
		"oauth_signature_method" = "HMAC-SHA1";
		"oauth_token" = f800fa5a68ae90acbcc7280a7d6ae90b;
		"oauth_token_secret" = 0bba4c1b602b2866403bc6fa848683e2;
		"oauth_verifier" = 0c83617a14946295efaf7115d5d906bd7a8a23708ee5bb0458b48a6edd9c3d9501d60fb78410ef95e431bc218bb2e9b1ffbe4017bea4ad77627962c5937b27cc746d329f814f37c5403768473b1c257d65c135736a3ec8d1bb9dc0630ca6fe1b4020c669c9fd22981b00ab881bac5687;
		"oauth_version" = "1.0";
	}
	NateOnProfile : {
		birthday = 1210;
		"birthday_type" = 2;
		id = "saturna@lycos.co.kr";
		"image_url" = "http://img.nateon.nate.com/web/getProfileFullImage.jsp?CMN=900788548";
		introduce = "";
		"login_type" = N;
		name = "\Uc774\Uacbd\Uad6c";
		nickname = "\Uc774\Uacbd\Uad6c";
		sex = 1;
	}
}
*/


+ (NSString*)generateOAuthNonce
{
	int nonce = (11111111 + arc4random() % (99999999-11111111+1));
	NSData* nonceData = [NSData dataWithBytes:&nonce length:sizeof(nonce)];

	NSMutableString* nonceBuffer = [[NSMutableString alloc] init];
	const unsigned char* dataBuffer = [nonceData bytes];

	for (int i=0; i < [nonceData length]; ++i)
	{
		[nonceBuffer appendFormat:@"%02X", (unsigned long)dataBuffer[i]];
	}
	//if (DEBUG) CSLog(@"===> nonce : [%@] / %d", nonceBuffer, [nonceBuffer length]);

	NSString* nonceString = [nonceBuffer md5];
	[nonceBuffer release];

	return nonceString;
}


#pragma mark -

- (BOOL)hasNateOnProfile
{
	NSMutableDictionary* dic = [m_profile objectForKey:kProfileKeyNateOn];
	return dic != nil && [dic count] > 0;
}

- (NSString*)nateOnProfileForKey:(NSString*)key
{
	NSMutableDictionary* dic = [m_profile objectForKey:kProfileKeyNateOn];
	return [dic objectForKey:key];
}

- (void)setNateOnProfile:(NSString*)profile forKey:(NSString*)key
{
	NSMutableDictionary* dic = [m_profile objectForKey:kProfileKeyNateOn];
	[dic setObject:profile forKey:key];
}


#pragma mark -

- (BOOL)hasCLogProfile
{
	NSMutableDictionary* dic = [m_profile objectForKey:kProfileKeyCLog];
	return dic != nil && [dic count] > 0;
}

- (NSString*)cLogProfileForKey:(NSString*)key
{
	NSMutableDictionary* dic = [m_profile objectForKey:kProfileKeyCLog];
	return [dic objectForKey:key];
}

- (void)setCLogProfile:(NSString*)profile forKey:(NSString*)key
{
	NSMutableDictionary* dic = [m_profile objectForKey:kProfileKeyCLog];
	[dic setObject:profile forKey:key];
}


@end
